<?php
//
//	file: includes/_cp_attach_fields.php
//	author: ptirhiik
//	begin: 15/02/2006
//	version: 1.6.1 - 09/02/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

function get_special_categories()
{
	return array(
		NONE_CAT => 'Not_assigned',
		IMAGE_CAT => 'Category_images',
		STREAM_CAT => 'Category_stream_files',
		SWF_CAT => 'Category_swf_files',
		DENIED_CAT => 'Category_denied',
	);
}

function get_download_modes()
{
	return array(
		INLINE_LINK => 'Link_inline',
		PHYSICAL_LINK => 'Link_physical',
	);
}

function attach_quota_limit_list($txt_none='')
{
	global $db;
	static $data;

	$txt_none = empty($txt_none) ? 'Not_assigned' : $txt_none;
	if ( !$data )
	{
		$data = array();
		$sql = 'SELECT quota_limit_id, quota_desc
					FROM ' . ATT_QUOTA_LIMITS_TABLE . '
					ORDER BY quota_limit ASC';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$data[ intval($row['quota_limit_id']) ] = $row['quota_desc'];
		}
		$db->sql_freeresult($result);
	}
	return array(0 => $txt_none) + $data;
}

function attach_get_user_quota($quota_type='upload', $check_exists=false)
{
	global $db, $view_user, $config;
	static $quotas, $done;

	if ( !$done )
	{
		$done = true;
		$quotas = array();
		$sql = 'SELECT quota_limit_id, quota_type
					FROM ' . ATT_QUOTA_TABLE . '
					WHERE group_id = ' . intval($view_user->data['group_id']);
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$type = intval($row['quota_type']) == QUOTA_UPLOAD_LIMIT ? 'upload' : 'pm';
			$quotas[$type] = doubleval($row['quota_limit_id']);
		}
		$db->sql_freeresult($result);
	}
	$dft = array(
		'upload' => doubleval($config->globals['mod_attachment_CH']['config']->data['default_upload_quota']),
		'pm' => doubleval($config->globals['mod_attachment_CH']['config']->data['default_pm_quota']),
	);
	return $check_exists ? isset($quotas[$quota_type]) : (isset($quotas[$quota_type]) ? doubleval($quotas[$quota_type]) : doubleval($dft[$quota_type]));
}

function get_extension_groups_list()
{
	global $db, $user;
	static $done, $data;

	if ( !$done )
	{
		$done = true;
		$data = array(0 => $user->lang('Not_assigned'));
		$sql = 'SELECT cat_id, ext_group_id, ext_group_name
					FROM ' . ATT_EXT_GROUPS_TABLE . '
					ORDER BY cat_id, ext_group_name, ext_group_id';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$data[ intval($row['ext_group_id']) ] = $row['ext_group_name'];
		}
		$db->sql_freeresult($result);
	}
	return $data;
}

class field_attach_dft_quota extends field_list
{
	function field_attach_dft_quota($field_name, &$data)
	{
		$data['options'] = attach_quota_limit_list('No_quota_limit');
		parent::field_list($field_name, $data);
	}

	function init()
	{
		parent::init();
		$this->type = 'list';
	}
}

class field_attach_quota extends field_list
{
	function field_attach_quota($field_name, &$data)
	{
		$data['options'] = attach_quota_limit_list('Not_assigned');
		parent::field_list($field_name, $data);
	}

	function init()
	{
		// get the original value
		$this->data['value'] = attach_get_user_quota($this->data['quota_type']);
		parent::init();
		$this->type = 'list';
	}

	function validate()
	{
		global $db, $view_user;

		$quota_type = $this->data['quota_type'] == 'upload' ? QUOTA_UPLOAD_LIMIT : QUOTA_PM_LIMIT;
		$exists = attach_get_user_quota($this->data['quota_type'], true);

		// delete
		if ( !$this->value )
		{
			if ( $exists )
			{
				$sql = 'DELETE FROM ' . ATT_QUOTA_TABLE . '
							WHERE group_id = ' . intval($view_user->data['group_id']) . '
								AND quota_type = ' . intval($quota_type);
				$db->sql_query($sql, false, __LINE__, __FILE__);
			}
		}

		// update
		else if ( $exists )
		{
			$sql = 'UPDATE ' . ATT_QUOTA_TABLE . '
						SET quota_limit_id = ' . intval($this->value) . '
						WHERE group_id = ' . intval($view_user->data['group_id']) . '
							AND quota_type = ' . intval($quota_type);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}

		// create
		else
		{
			$fields = array(
				'group_id' => intval($view_user->data['group_id']),
				'quota_type' => intval($quota_type),
				'quota_limit_id' => intval($this->value),
			);
			$sql = 'INSERT INTO ' . ATT_QUOTA_TABLE . '
						(' . $db->sql_fields('fields', $fields) . ') VALUES(' . $db->sql_fields('values', $fields) . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
	}
}

class field_attach_filesize extends field
{
	var $fields;

	function field_attach_filesize($name, &$data)
	{
		$value = isset($data['value']) ? $data['value'] : 0;
		$units = array();
		$unit = size_get_units($value, $units);
		$fields = array(
			$name . '_size' => array_merge($data, array('type' => 'int', 'value' => round($value / $unit))),
			$name . '_unit' => array('type' => 'list', 'combined' => true, 'output' => isset($data['output']) && $data['output'], 'options' => $units, 'value' => $unit),
		);
		$this->fields = array();
		foreach ( $fields as $field_name => $field )
		{
			$field_class = 'field_' . $field['type'];
			$this->fields[$field_name] = new $field_class($field_name, $field);
		}
		parent::field($name, $data);
	}

	function get_value($value)
	{
		$size = intval($this->fields[ $this->name . '_size' ]->get_value($this->fields[ $this->name . '_size' ]->data['value']));
		$unit = intval($this->fields[ $this->name . '_unit' ]->get_value($this->fields[ $this->name . '_unit' ]->data['value']));
		return $size * $unit;
	}

	function check()
	{
		global $error, $error_msg;
		if ( $this->data['output'] )
		{
			return;
		}
		// do basic checks
		foreach ($this->fields as $field_name => $field)
		{
			$field->check();
		}
	}

	function display()
	{
		foreach ($this->fields as $field_name => $field)
		{
			$field->display();
		}
	}
}

class field_attach_search_username extends field_varchar
{
	function init()
	{
		parent::init();
		$this->type = 'varchar';
	}

	function get_value($value)
	{
		return $this->encode(_read($this->name, TYPE_HTML, $this->decode($value)));
	}

	function encode($value)
	{
		return stripslashes(phpbb_clean_username(addslashes(trim(preg_replace('#\s+#', ' ', $value)))));
	}
}

?>